{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "#提取每日总购买和赎回金额\n",
    "balance_table=pd.read_csv('./Purchase Redemption Data/user_balance_table.csv')\n",
    "total_capital_data=pd.DataFrame(data=balance_table,columns=['report_date','total_purchase_amt','total_redeem_amt'])\n",
    "# total_capital_data.head()\n",
    "total_capital_data.to_csv('total_amount_data.csv',index=False,header=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "#提取用户id，direct_purchase_amt,total_redeem_amt\n",
    "user_balance_table=pd.read_csv('./Purchase Redemption Data/user_balance_table.csv')\n",
    "user_active_data=pd.DataFrame(data=user_balance_table,columns=['user_id','direct_purchase_amt','total_redeem_amt'])\n",
    "user_active_data.to_csv('user_active_data.csv',index=False,header=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "#读取txt文件\n",
    "with open('task1_output.txt', 'r') as txt_file, open('task1_output2.csv', 'w') as csv_file:\n",
    "    csv_file.write('date,purchase_amt,redeem_amt\\n')\n",
    "    # 遍历TXT文件的每一行\n",
    "    for line in txt_file:\n",
    "        # 移除行尾的换行符，用逗号替换制表符\n",
    "        csv_line = line.strip().replace('\\t',\",\") + '\\n'\n",
    "        # 写入CSV文件\n",
    "        csv_file.write(csv_line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=pd.read_csv('task2_output.csv')\n",
    "df.sort_values(by='total_purchase_amt',ascending=False,inplace=True)\n",
    "df.reset_index(drop=True,inplace=True)\n",
    "df['output']=df['weekday']+'\\t'+df['total_purchase_amt'].astype(str)+','+df['total_redeem_amt'].astype(str)\n",
    "dataout=pd.DataFrame(data=df,columns=['output'])\n",
    "dataout.to_csv('task2_output.csv',index=False,header=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=pd.read_csv('task1_output.csv')\n",
    "df\n",
    "df['output']=df['date'].astype(str)+'\\t'+df['total_purchase_amt'].astype(str)+','+df['total_redeem_amt'].astype(str)\n",
    "dataout=pd.DataFrame(data=df,columns=['output'])\n",
    "dataout.to_csv('task1_output.csv',index=False,header=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "#读取txt文件\n",
    "with open('task4_output.txt', 'r') as txt_file, open('task4_output.csv', 'w') as csv_file:\n",
    "    csv_file.write('week_num,bank_rate,bao_rate,purchase_avg,redeem_avg\\n')\n",
    "    # 遍历TXT文件的每一行\n",
    "    for line in txt_file:\n",
    "        # 移除行尾的换行符，用逗号替换制表符\n",
    "        csv_line = line.strip().replace('\\t',\",\") + '\\n'\n",
    "        # 写入CSV文件\n",
    "        csv_file.write(csv_line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=pd.read_csv('task4_output.csv')\n",
    "#按照week_num排列\n",
    "data.sort_values(by='week_num',ascending=True,inplace=True)\n",
    "data.reset_index(drop=True,inplace=True)\n",
    "data.to_csv('task4_output.csv',index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
